import React from 'react';
import {Router, Route, Link} from 'react-router';

import utils from '../../js/utils';
import config from '../../../config/config';

class Login extends React.Component {
  constructor () {
    super();
  }

  componentDidMount () {
    let $submit = $('#submit');

    // 检测值是否存在
    function checkValue (value) {
      if (parseInt(value) == 0) {
        return true;
      }
      return ((value == "" || value == false || value == null || value == undefined) ? false : true);
    };

    // 检测是否是数字
    function checkRate(input) {
    　var re = /^[0-9]+.?[0-9]*/;//判断字符串是否为数字//判断正整数/[1−9]+[0−9]∗]∗/
      return (!re.test(input) ? false : true);
    }

    var submit_login = function () {
      var uname = $("#uname").val().trim().toString();
      var password = $('#password').val().trim().toString();

      if (!checkValue(uname)) {
        $('#uname').siblings('p[class="alert-account"]').text("手机号码 / 帐号不能为空").show();
        $('#uname').addClass('warning-account');
        return;
      }

      $('#uname').siblings('p[class="alert-account"]').text("").hide();
      $('#uname').removeClass('warning-account');

      if (!checkValue(password)) {
        $('#password').siblings('p[class="alert-account"]').text("请输入密码").show();
        $('#password').addClass('warning-account');
        return;
      }

      $('#password').siblings('p[class="alert-account"]').text("").hide();
      $('#password').removeClass('warning-account');

      utils.post("/check_admin_login", {uname, pwd: password}, function (data) {

        if (parseInt(data.code, 10) == 200) {
          $.cookie('admin_token', data.token, { expires: 1, path: '/'});
          $('#authurl').attr('href', data.url);
          $('#authurl')[0].click();
          return;
        }

        if (parseInt(data.code, 10) == 50003) {
          $('#uname').siblings('p[class="alert-account"]').text(data.errmsg).show();
          $('#uname').addClass('warning-account');
          return;
        }

        if (parseInt(data.code, 10) == 50002) {
          $('#password').siblings('p[class="alert-account"]').text(data.errmsg).show();
          $('#password').addClass('warning-account');
          return;
        }
      })
    }

    $submit.click(function () {
      submit_login();
    });

    $('input').keydown(function(event){
      if (parseInt(event.keyCode, 10) == 13) {
        submit_login();
      }
    });
  }

  render () {
    return (
      <div className="xlb-account-login">
        <div>
          <h3>用户登录</h3>
          <div>
            <input type="text" id="uname" placeholder="请输入手机号码 / 帐号" />
            <p className="alert-account"></p>
          </div>
          <div>
            <input type="password" id="password" placeholder="请输入密码" />
            <p className="alert-account"></p>
          </div>
          <a className="login-btn" id="submit" href="javascript:void(0)">登录</a>
        </div>
        <a id="authurl"></a>
      </div>
    )
  }
}

export default Login;
